#!/usr/bin/python
# vi: et sw=2
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#

from lib_pulseaudio import *
import math

class PulseVolume:
  def __init__(self, vol = 0, channels = 2):
    self.channels = channels
    if vol > 100 or vol < 0:
      print "WARNING: Volume is invalid!"
      vol = 0
    self.values   = [vol] * self.channels
    return

  ##############################
  #
  # Type conversions
  #
  def fromCtypes(self, pa_cvolume):
    self.channels = pa_cvolume.channels
    self.values   = map(lambda x: (math.ceil(float(x) * 100 / PA_VOLUME_NORM)),
                        pa_cvolume.values[0:self.channels])
    return self

  def toCtypes(self):
    ct = PA_CVOLUME()
    ct.channels = self.channels

    for x in range(0, self.channels):
      ct.values[x] = (self.values[x] * PA_VOLUME_NORM) / 100
    
    return ct

  ###

  def printDebug(self):
    print "PulseVolume"
    print "self.channels:", self.channels
    print "self.values:", self.values
    #print "self.proplist:", self.proplist

  def setVolume(self, vol, balance = None):
    if not balance:
      self.values = [vol] * self.channels
    else:
      self.values[balance] = vol
    return

  def __str__(self):
    return "Channels: " + str(self.channels) + \
           ", values: \"" + str(map(lambda x: str(x) + "%", self.values)) + "\""
